var app = getApp()
Page({
    data:{
		root:'',
		siteroot:app.setting.siteroot,
		att_url:app.setting.att_url,
        pay_id:'',
		back_url:'',
		error:false,
		success:false,
		nodatapic_close:true,
    },
	onPullDownRefresh: function (){
		var the=this;
		setTimeout(function(){
			wx.stopPullDownRefresh()
		},1000);
	},
    onLoad:function(options){
        var the = this
        var pay_id = parseInt(options.pay_id);
		var di_open =options.di_open?options.di_open:'';
		var manage = parseInt(options.manage);
		//console.log('manage');console.log(manage)
		wx.setNavigationBarTitle({'title':'支付页面'});
		app.util.showLoading();
		//判断是否来自管理端
		var oldmanage=app.setting.manage==1?1:0;
		if(manage==1){
			app.setting.index_url_new=app.setting.siteroot+'manage/index.php?wxgid='+app.setting.wxgid
			app.setting.api_url_new=app.setting.siteroot+'api.php?this=manage'
			app.setting.manage=1;
		}else{
			app.setting.index_url_new=app.setting.siteroot+'custom/index.php?wxgid='+app.setting.wxgid
			app.setting.api_url_new=app.setting.siteroot+'api.php?this=custom'
			app.setting.manage=0;
		}
		if(pay_id==0){
			the.setData({
				error:true
			});
			app.util.msg('订单为空！');
			return false;
		}
		//客户端切换成管理端了
		if(manage != oldmanage){
			app.util.session(function (res){
				if(res==0){
					app.util.msg('获取回话失败！');
				}else{
					the.gopay(pay_id,di_open);
				}
			});
		}else{
			the.gopay(pay_id,di_open)
		}
    },
	gopay:function(pay_id,di_open){
		var the = this
		app.util.pay(pay_id,function(res){
			app.util.hideLoading();
			if(res.type=='success'){
				var success_url=res.success_url?res.success_url:'';
				the.setData({
					back_url:success_url,
					success:true,
					error:false,
				});
				app.util.msg(res.message);
				if(success_url){
					success_url = encodeURIComponent(success_url);
					wx.navigateTo({'url':'/pages/html/html?url='+success_url});
				}else{
					wx.navigateTo({'url':'/pages/index/index'});
				}
			}else{
				var error_url=res.error_url?res.error_url:'';
				//console.log('error_url');console.log(error_url);
				the.setData({
					back_url:error_url,
					error:true,
					success:false,
				});
				app.util.msg(res.message);
			}
		},1,'',di_open);
	},
	goback:function(){
		var the = this
		var url = the.data.back_url
		console.log('goback.url');console.log(url);
		if(url){
			url = encodeURIComponent(url);
			wx.navigateTo({'url':'/pages/html/html?url='+url});
		}else{
			wx.navigateTo({'url':'/pages/index/index'});
		}
		
	}
})